package com.example.ruralservicedigitalplatform.service.impl;

import com.example.ruralservicedigitalplatform.constant.MessageConstant;
import com.example.ruralservicedigitalplatform.context.BaseContext;
import com.example.ruralservicedigitalplatform.domain.dto.OrderSubmitDto;
import com.example.ruralservicedigitalplatform.domain.dto.OrdersPageQueryDto;
import com.example.ruralservicedigitalplatform.domain.po.*;
import com.example.ruralservicedigitalplatform.domain.vo.OrderSubmitVO;
import com.example.ruralservicedigitalplatform.exception.AddressBookBusinessException;
import com.example.ruralservicedigitalplatform.exception.ShoppingCartBusinessException;
import com.example.ruralservicedigitalplatform.mapper.AddressBookMapper;
import com.example.ruralservicedigitalplatform.mapper.OrderMapper;
import com.example.ruralservicedigitalplatform.mapper.ShoppingCartMapper;
import com.example.ruralservicedigitalplatform.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 用户下单
     * @param orderSubmitDto
     * @return
     */
    @Transactional
    @Override
    public OrderSubmitVO submitOrder(OrderSubmitDto orderSubmitDto) {
        //处理各种业务异常(地址、购物车为空)
        AddressBook addressBook = addressBookMapper.selectById(orderSubmitDto.getAddressId());
        if (addressBook == null) {
            //地址簿为空异常
            throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setVillagerId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartlist = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartlist == null || shoppingCartlist.size() == 0) {
            //购物车为空异常
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }



        //向订单插入一条数据
        Orders orders = new Orders();
        BeanUtils.copyProperties(orderSubmitDto, orders);
        orders.setAddress(addressBook.getAddressName()+addressBook.getAddressDetail());

        orders.setOrderTime(LocalDateTime.now());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setOrderStatus(Orders.PENDING_PAYMENT);
        orders.setOrderNumber(String.valueOf(System.currentTimeMillis()));
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setVillagerId(BaseContext.getCurrentId());
        //插入订单数据
        orderMapper.insert(orders);
        //清空购物车
        shoppingCartMapper.deleteByVillagerId(BaseContext.getCurrentId());

        Summary summary = shoppingCartMapper.selectByVillagerId(BaseContext.getCurrentId());
        OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
                .orderId(orders.getOrderId())
                .orderTime(orders.getOrderTime())
                .totalPrice(summary.getTotal())
                .orderNumber(orders.getOrderNumber())
                .build();
        return orderSubmitVO;
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param orderStatus
     * @return
     */

    @Override
    public PageResult pageQueryUser(int page, int pageSize, Integer orderStatus) {
        //设置分页
        PageHelper.startPage(page,pageSize);
        OrdersPageQueryDto ordersPageQueryDto = new OrdersPageQueryDto();
        ordersPageQueryDto.setVillagerId(BaseContext.getCurrentId());
        ordersPageQueryDto.setOrderStatus(orderStatus);

        //分页条件查询
        Page<Orders> page1 = orderMapper.pageQuery(ordersPageQueryDto);
        List<Orders> list = page1.getResult();
        return new PageResult(page1.getTotal(), list);
    }


}
